home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / printernamecombobox.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  4.7 KB  |  147 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. # Std Lib
  23. #import sys
  24.  
  25. # Local
  26. from base.g import *
  27. from ui_utils import *
  28. from base import device
  29.  
  30. # Qt
  31. from PyQt4.QtCore import *
  32. from PyQt4.QtGui import *
  33.  
  34.  
  35. PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY = 0
  36. PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY = 1
  37. PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX = 2
  38.  
  39.  
  40. class PrinterNameComboBox(QWidget):
  41.     def __init__(self, parent):
  42.         QWidget.__init__(self, parent)
  43.         self.printer_name = ''
  44.         self.device_uri = ''
  45.         self.printer_index = {}
  46.         self.initial_printer = None
  47.         self.updating = False
  48.         self.typ = PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY
  49.         self.initUi()
  50.  
  51.  
  52.     def initUi(self):
  53.         #print "PrinterNameComboBox.initUi()"
  54.         HBoxLayout = QHBoxLayout(self)
  55.         HBoxLayout.setObjectName("HBoxLayout")
  56.  
  57.         self.NameLabel = QLabel(self)
  58.         self.NameLabel.setObjectName("NameLabel")
  59.         HBoxLayout.addWidget(self.NameLabel)
  60.  
  61.         SpacerItem = QSpacerItem(20, 20, QSizePolicy.Minimum, QSizePolicy.Minimum)
  62.         HBoxLayout.addItem(SpacerItem)
  63.  
  64.         self.ComboBox = QComboBox(self)
  65.         sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
  66.         sizePolicy.setHorizontalStretch(0)
  67.         sizePolicy.setVerticalStretch(0)
  68.         sizePolicy.setHeightForWidth(self.ComboBox.sizePolicy().hasHeightForWidth())
  69.         self.ComboBox.setSizePolicy(sizePolicy)
  70.         self.ComboBox.setObjectName("ComboBox")
  71.         HBoxLayout.addWidget(self.ComboBox)
  72.  
  73.         self.NameLabel.setText(self.__tr("Printer:"))
  74.  
  75.         #self.connect(self.ComboBox, SIGNAL("currentIndexChanged(int)"),
  76.         #    self.ComboBox_currentIndexChanged)
  77.  
  78.         self.connect(self.ComboBox, SIGNAL("currentIndexChanged(const QString &)"),
  79.             self.ComboBox_currentIndexChanged)
  80.  
  81.     def setType(self, typ):
  82.         if typ in (PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY,
  83.                    PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY,
  84.                    PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX):
  85.             self.typ = typ
  86.  
  87.  
  88.     def setInitialPrinter(self, printer_name):
  89.         self.initial_printer = printer_name
  90.  
  91.  
  92.     def updateUi(self):
  93.         #print "PrinterNameComboBox.updateUi()"
  94.         if self.typ == PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY:
  95.             self.NameLabel.setText(self.__tr("Printer Name:"))
  96.             be_filter = ['hp']
  97.  
  98.         elif self.typ == PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY:
  99.             self.NameLabel.setText(self.__tr("Fax Name:"))
  100.             be_filter = ['hpfax']
  101.  
  102.         else: # PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX
  103.             self.NameLabel.setText(self.__tr("Printer/Fax Name:"))
  104.             be_filter = ['hp', 'hpfax']
  105.  
  106.         self.printers = device.getSupportedCUPSPrinters(be_filter)
  107.         self.printer_index.clear() # = {}
  108.  
  109.         if self.printers:
  110.             if self.initial_printer is None:
  111.                 self.initial_printer = user_conf.get('last_used', 'printer_name')
  112.  
  113.             self.updating = True
  114.             try:
  115.                 k = 0
  116.                 for i, p in enumerate(self.printers):
  117.                     self.printer_index[p.name] = p.device_uri
  118.                     self.ComboBox.insertItem(i, p.name)
  119.  
  120.                     if self.initial_printer is not None and p.name == self.initial_printer:
  121.                         self.initial_printer = None
  122.                         k = i
  123.  
  124.                 self.ComboBox.setCurrentIndex(-1)
  125.  
  126.             finally:
  127.                 self.updating = False
  128.  
  129.             self.ComboBox.setCurrentIndex(k)
  130.         else:
  131.             self.emit(SIGNAL("PrinterNameComboBox_noPrinters"))
  132.  
  133.  
  134.     def ComboBox_currentIndexChanged(self, t):
  135.         self.printer_name = unicode(t)
  136.  
  137.         if self.updating:
  138.             return
  139.  
  140.         self.device_uri = self.printer_index[self.printer_name]
  141.         user_conf.set('last_used', 'printer_name', self.printer_name)
  142.         self.emit(SIGNAL("PrinterNameComboBox_currentChanged"), self.device_uri, self.printer_name)
  143.  
  144.  
  145.     def __tr(self,s,c = None):
  146.         return qApp.translate("PrinterNameComboBox",s,c)
  147.